package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Settings;
import com.tanhua.dubbo.mapper.SettingsMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SettingsApiImpl implements SettingsApi {
    //注入对应的映射对象
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 通过用户id查询设置信息
     * @param id
     * @return
     */
    @Override
    public Settings findById(Long id) {
        //创建条件查询器
        QueryWrapper<Settings> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",id);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 更新用户信息--我的通知
     * @param settings
     */
    @Override
    public void save(Settings settings) {
        //通过用户id查询通知设置
        //SettingsInUser:数据库里面的 Settings
        Settings SettingsInUser = findById(settings.getUserId()); //查询这个用户是更新还是第一次添加
        if(SettingsInUser==null){
            //如果不存在直接添加
            settingsMapper.insert(settings);
        }else {
            //存在则更新
            settings.setId(SettingsInUser.getId());
            settingsMapper.updateById(settings);
        }
    }


}
